import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/bean/edc_data_entity.dart';
import 'package:xin_nuo_pda/values/constant.dart';
import 'package:xin_nuo_pda/views/SubTitleBar.dart';
import 'package:xin_nuo_pda/views/TextView.dart';
import 'package:xin_nuo_pda/views/edittext/ClearEditText.dart';
import 'package:xin_nuo_pda/views/edittext/TitleEditText.dart';
import 'package:xin_nuo_pda/utils/string_extension.dart';
import 'package:xin_nuo_pda/values/colors.dart';
import 'DataUploadController.dart';

class DataUploadPage extends StatelessWidget {
  final controller = Get.put(DataUploadController());
  final state = Get
      .find<DataUploadController>()
      .state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(USER),
        ),
        body: SingleChildScrollView(
          child: GetBuilder<DataUploadController>(
            builder: (controller) =>
                Container(
                  child: Column(
                    children: [
                      SubTitleBar("测试站数据上报", '提交', () {
                        controller.report();
                      }),
                      TitleEditText(
                        "批次ID:",
                        "请扫描批次ID",
                            (value) {
                          controller.getLotInfo(value);
                        },
                      ),
                      GetBuilder<DataUploadController>(
                        builder: (controller) =>
                            Column(
                              children: [
                                TextView(
                                    "设备ID(主):",
                                    state.lotEntity?.eQUIPMENTID ?? ''),
                                TextView("批次数量:",
                                    state.lotEntity?.mAINQTY.toString() ?? '0'),
                                Column(
                                  children: [
                                    Container(
                                      margin: EdgeInsets.all(8),
                                      alignment: Alignment.centerLeft,
                                      child: Text(
                                        "BIN信息",
                                        style: TextStyle(
                                            fontWeight: FontWeight.bold,
                                            fontSize: 16),
                                      ),
                                    ),
                                    Table(
                                      defaultVerticalAlignment:
                                      TableCellVerticalAlignment.middle,
                                      columnWidths: {
                                        //列宽
                                        0: FlexColumnWidth(1.0),
                                        1: FlexColumnWidth(1.0),
                                        2: FlexColumnWidth(2.0),
                                        // 3: FlexColumnWidth(1.0),
                                      },
                                      border: TableBorder.all(
                                        color: Colors.grey,
                                        width: 1.0,
                                        style: BorderStyle.solid,
                                      ),
                                      children: getTableList(context),
                                    )
                                  ],
                                ),
                              ],
                            ),
                        id: state.upload,
                      ),
                    ],
                  ),
                ),
          ),
        ));
  }

  List<TableRow> getTableList(BuildContext context) {
    List<TableRow> tempView = [];
    List<FocusNode> focusNodeList = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                'BIN名称',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              'BIN 描述',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '数量',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          // Align(
          //   alignment: Alignment.center,
          //   child: Text(
          //     '占比良率',
          //     style: TextStyle(
          //       color: theme_blue.toColor(),
          //     ),
          //   ),
          // ),
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));

    if (state.edcData == null) {
      state.edcData = [];
    }
    for (int i = 0; i < state.edcData!.length; i++) {
      EdcDataEDCSETLISTEDCSETBINLINELIST element = state.edcData![i];
      focusNodeList.add(FocusNode());
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              element.nAME ?? '',
              style: TextStyle(
                color: text_black.toColor(),
              ),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.dESCRIPTION ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: ClearEditText(
            "",
                (value) {
              element.VALUE = value;
              // if(value.isNotEmpty){
              //   element.check =
              //       ((value.toInt() / (state.lotEntity!.mAINQTY ?? 0)) * 100).toString() +
              //           '%';
              // }else{
              //   element.check = "0%";
              // }
              // controller.update([state.binListInfo]);
            },
            isScan: false,
            controller: TextEditingController(text: element.VALUE ?? ''),
          ),
        ),
        // GetBuilder<DataUploadController>(builder: (controller) {
        //   return Align(
        //     alignment: Alignment.center,
        //     child: Text(
        //       element.check ?? '0%',
        //       style: TextStyle(
        //         color: text_black.toColor(),
        //       ),
        //     ),
        //   );
        // },id: state.binListInfo,),
      ]));
    }
    return tempView;
  }
}
